package com.cdshangpai.photoapp.ui.caseui;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.cdshangpai.photoapp.R;
import com.cdshangpai.photoapp.databinding.ImageListItemBinding;
import com.cdshangpai.photoapp.ui.OnListViewClickListener;
import com.cdshangpai.utils.db.beans.Video;
import com.cdshangpai.utils.storage.ImageManager;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> {
    ImageManager imageManager=new ImageManager();
    Context context;
    List<Video> datas=new ArrayList<>();
    public OnListViewClickListener onListViewClickListener;

    public VideoListAdapter(Context context,OnListViewClickListener onListViewClickListener)
    {
        this.context=context;
        this.onListViewClickListener=onListViewClickListener;
    }

    @NonNull
    @NotNull
    @Override
    public VideoListAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        ImageListItemBinding binding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.image_list_item,parent,false);

        View view=binding.getRoot();

        /*ViewGroup.MarginLayoutParams mlp=new ViewGroup.MarginLayoutParams(view.getLayoutParams());
        mlp.leftMargin= ScreenParams.colmargin;
        mlp.rightMargin= ScreenParams.colmargin;
        mlp.bottomMargin=ScreenParams.rowmargin;
        mlp.width=ScreenParams.iconWidth;
        mlp.height=ScreenParams.iconHeight;

        FlexboxLayoutManager.LayoutParams lps=new FlexboxLayoutManager.LayoutParams(mlp);
        view.setLayoutParams(lps);*/

        VideoListAdapter.ViewHolder viewHolder=new VideoListAdapter.ViewHolder(view,binding);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull VideoListAdapter.ViewHolder holder, int position) {
        Video video=datas.get(position);
        holder.position=position;
        ImageListItemBinding binding=holder.binding;
        Bitmap temp=imageManager.loadImageToBitmap(video.titleimg);
        Bitmap bitmap=imageManager.scale(temp,400);
        binding.imageListItemImg.setImageBitmap(bitmap);
        binding.imageListItemTitle.setText(video.title);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public Integer position;
        ImageListItemBinding binding;
        public ViewHolder(View view, ImageListItemBinding binding)
        {
            this(view);
            this.binding=binding;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    _onClick(v,position);
                }
            });
        }
        public ViewHolder(View view)
        {
            super(view);
        }

        final void  _onClick(View view,Integer position)
        {
            onListViewClickListener.onClick(position);
        }
    }


}
